package com.itboye.mengwu.app.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.SPUtils;
import com.itboye.core.utils.XImageLoader;
import com.itboye.inageview.ImagePagerActivity;
import com.itboye.inageview.NoScrollGridView;
import com.itboye.inageview.RoundImageView;
import com.itboye.mengwu.R;
import com.itboye.mengwu.data.model.ShopPingJiabean;
import com.itboye.mengwu.widget.GridViewNameAdapter;

public class ShoPingAdapter extends BaseAdapter {
	private Context mContext;
	private ShopPingJiabean items;
	private OnClickListener clickListener;

	public ShoPingAdapter(Context mContext, ShopPingJiabean items) {
		this.items = items;
		this.mContext = mContext;
	}
	public void  upDate(Context mContext, ShopPingJiabean items) {
		this.items = items;
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return items.getList() == null ? 0 : items.getList().size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		// houderView holder;
		houderView holder;
		if (convertView == null) {
			holder = new houderView();
			convertView = View.inflate(mContext, R.layout.item_shangping, null);
			holder.imgMePingJia = (RoundImageView) convertView
					.findViewById(R.id.imgMePingJia);
			holder.tv_Context = (TextView) convertView
					.findViewById(R.id.tv_Context);
			holder.txtPinJiaTime = (TextView) convertView
					.findViewById(R.id.txtPinJiaTime);
			holder.gridview = (NoScrollGridView) convertView
					.findViewById(R.id.mepingjiagridview);

			convertView.setTag(holder);
		} else {
			holder = (houderView) convertView.getTag();
		}

		holder.txtPinJiaTime.setText(new SimpleDateFormat("yyyy-MM-dd")
				.format(Long.parseLong(items.getList().get(position)
						.getCreatetime()) * 1000));
		XImageLoader.load(
				SPUtils.get(mContext, null, SpPublicParams.USERHEAD, "") + "",
				holder.imgMePingJia);

		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>"
				+ items.getList().get(position).getComment());

		final ArrayList<String> imageUrls = items.getList().get(position)
				.getImg();
		if (imageUrls == null || imageUrls.size() == 0) { // 没有图片资源就隐藏GridView
			holder.gridview.setVisibility(View.GONE);
		} else {
			holder.gridview.setAdapter(new GridViewNameAdapter(mContext,
					imageUrls));
			holder.gridview.setVisibility(View.VISIBLE);
		}
		// 点击回帖九宫格，查看大图
		holder.gridview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				imageBrower(position, imageUrls);
			}
		});
//		String TDSUDP = "【已评价】" + items.getList().get(position).getComment();
//		;
//
//		int length = items.getList().get(position).getComment().length();
//		if (length == 4) {
//			// TDSUDP = "妈妈赠" + TDSUDP;
//			SpannableStringBuilder style = new SpannableStringBuilder(TDSUDP);
//			style.setSpan(new ForegroundColorSpan(Color.parseColor("#8FFBFD")),
//					0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//			holder.tv_Context.setText(style);
//		}
		holder.tv_Context.setText(items.getList().get(position).getComment());
		return convertView;
	}

	/**
	 * 打开图片查看器
	 * 
	 * @param position
	 * @param urls2
	 */
	protected void imageBrower(int position, ArrayList<String> urls2) {
		Intent intent = new Intent(mContext, ImagePagerActivity.class);
		// 图片url,为了演示这里使用常量，一般从数据库中或网络中获取
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_URLS, urls2);
		intent.putExtra(ImagePagerActivity.EXTRA_IMAGE_INDEX, position);
		mContext.startActivity(intent);
	}

	class houderView {
		private TextView txtYiPinJia, txtPinJiaTime, txtMiaoshu, txtWuLiu,
				txtZengSong;
		private RoundImageView imgMePingJia;
		private NoScrollGridView mepingjiagridview;

		// ///
		private ImageView iv_avatar;
		private TextView tv_Context;
		private TextView tv_content;
		private NoScrollGridView gridview;

	}

}
